<template>
	<xn-form-container
		:title="formData.productTypeId ? '编辑商品分类' : '增加商品分类'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="上级类型：" name="parentId">
						<a-tree-select
							v-model:value="formData.parentId"
							show-search
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择上级类型"
							allow-clear
							tree-default-expand-all
							:tree-data="topTypeData"
							:field-names="{
							  children: 'children',
							  label: 'productTypeName',
							  value: 'productTypeId',
							}"
							tree-node-filter-prop="productTypeName"
						></a-tree-select>

					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="分类名称：" name="productTypeName">
						<a-input v-model:value="formData.productTypeName" placeholder="请输入分类名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="显示顺序：" name="orderNum">
						<a-input-number v-model:value="formData.orderNum" :min="1" :max="10000" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="状态：" name="status">
						<a-select v-model:value="formData.status" placeholder="请选择状态" :options="statusOptions" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="imsProductTypeForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import imsProductTypeApi from '@/api/ims/imsProductTypeApi'





	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		orderNum: 50,
		status: 'ENABLE',
		parentId: '1'

	})
	const submitLoading = ref(false)
	const statusOptions = ref([])

	const topTypeData = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		statusOptions.value = tool.dictList('COMMON_STATUS')

		// 上级分类
		imsProductTypeApi.productTypeTree().then(res => {
			topTypeData.value = res
			console.log('树选择器：', res)
		})


	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value = formRef.value.resetFields()
		formData.value = {
			orderNum: 50,
			status: 'ENABLE',
			parentId: '1'

		}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		productTypeName: [required('请输入分类名称')],
		status: [required('请输入状态')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				imsProductTypeApi
					.imsProductTypeSubmitForm(formDataParam, formDataParam.productTypeId)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
